<?php
namespace Macaca\Controller\Admin;

use Macaca;
use Uploader\Uploader;

class Goods extends Base {

    public function moduleGoods() {
        //'g_id','gc_id','g_name','captain','detail','image','price','keywords','description','add_time','top_sort','click'
        return new Macaca\Module\Admin\Goods($this->app);
    }

    public function actionGetList() {
        $app = $this->app;
        $view = new Macaca\View\Admin\Goods($app);
        $module = $this->moduleGoods();
        $linker = $this->linker;

        $data['title'] = 'Macaca 商品管理';
        $data['url_add'] = $linker->toFix('/admin/goods/add');
        $data['url_edit'] = $linker->to('/admin/goods/edit');
        $data['url_delete'] = $linker->to('/admin/goods/delete');
        $data['url_browse'] = $linker->to('/admin/goods/browse');

        $page = $linker->router->getParameter('page', 0);
        $pageUrlPre = $linker->to('/admin/goods/');
        $itemCount = $module->getCount();
        $data['page_box'] =
            $view->view->page()
            ->setUrlPre($pageUrlPre)->setCurrentLayout('first-pre-list-next-last-total')
            ->getByList($page, $itemCount, 10);

        $data['goods'] = $module->getGoodsList($page, 10);
        $this->response->setBody($view->render($data));
    }

    public function actionAddItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\GoodsEditor($app);
        $module = $this->moduleGoods();
        $moduleCategory = new Macaca\Module\Admin\GoodsCategory($this->app);

        $data['title'] = '添加商品';
        $data['url_save'] = $this->linker->toFix('/admin/goods/save');
        $data['url_upload'] = $this->linker->toFix('/admin/goods/upload/image');

        $data['goods'] = $module->getEmptyItem();
        $data['category_list'] = $moduleCategory->getCategoryList(0);
        $data['is_top'] = false;

        $this->response->setBody($view->render($data));
    }

    public function actionGetItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\GoodsEditor($app);
        $module = $this->moduleGoods();
        $moduleCategory = new Macaca\Module\Admin\GoodsCategory($this->app);

        $data['title'] = '编辑商品';
        $data['url_save'] = $this->linker->toFix('/admin/goods/save');
        $data['url_upload'] = $this->linker->toFix('/admin/goods/upload/image');

        $gId = $app->router()->getParameter('g_id');
        $data['goods'] = $module->getItemById($gId);

        if (empty($data['goods'])) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;你要编辑的商品不存在！';
        }
        else {
            $data['category_list'] = $moduleCategory->getCategoryList($data['goods']['gc_id']);
            $data['is_top'] = (($this->app->TIME - $data['goods']['top_sort']) < 5 * 24 * 60 * 60);
            $this->response->setBody($view->render($data));
        }
    }

    public function actionDeleteItem() {
        $module = $this->moduleGoods();
        $gId = $this->app->router()->getParameter('g_id');

        if ($module->deleteItemById($gId)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;成功删除所选商品。';
        }
        else {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;商品删除失败!';
        }
    }

    public function actionBrowseItem() {
        echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;该功能未实现!';

    }

    public function actionSaveItem() {
        $module = $this->moduleGoods();
        if (stripos($_POST['image'], '/imageTemp/')) {
            $oldName = url_to_file($_POST['image']);
            $_POST['image'] = str_ireplace('/imageTemp/', '/image/', $_POST['image']);
            $newName = url_to_file($_POST['image']);
            dir_check(dirname($newName));
            rename($oldName, $newName);
        }

        if (isset($_POST['is_top']) and strtolower($_POST['is_top']) == 'on') {
            $_POST['top_sort'] = $this->app->TIME;
        }
        else {
            $_POST['top_sort'] = 0;
        }

        if ($module->saveItem($_POST)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;商品保存成功!';
        }
        else {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;商品保存失败!';
        }

    }

    public function actionUploadImage() {
        $allowedExtensions = array("jpeg", "jpg", "png", "bmp");
        $sizeLimit = 10 * 1024 * 1024;
        $dir = $this->app->FRONT_ROOT_DIR . '/goods/imageTemp'; //写入临时文件夹以定期清除上传但是为保存的商品图片
        $reFile = date('Y-m-d-h-i-s_', $this->app->TIME) . $this->app->request()->getIP();
        $uploader = new Uploader($allowedExtensions, $sizeLimit);
        $result = $uploader->saveToDir($dir, $reFile);
        isset($result['filename']) and $result['filename'] = file_to_url($result['filename']);
        echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
    }
} 